package com.zapi.form.model.dto;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zapi.enums.FormFieldTypeEnum;
import com.zapi.form.model.entity.Form;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/***
 * 数据表字段
 * @author zhonglz
 * @date 2025年08月19日16:32:15
 */
@Data
public class FormTableColumnDTO {

    /***
     * 表单模板ID
     */
    private String formId;
    /***
     * 表名称
     */
    private String tableName;

    /***
     * 表描述
     */
    private String tableComment;


    /****
     * 表字段集合
     */
    private List<FormColumnInfoDTO> columnInfoDTOList;

    /***
     * 表单信息
     */
    private Form formInfo;


    public FormTableColumnDTO(Form formInfo){
        this.formId = formInfo.getId();
        this.tableName = formInfo.getId();
        this.tableComment = formInfo.getFormName();
        this.formInfo = formInfo;
        this.columnInfoDTOList = parseContentToColumnInfoList(formInfo.getFormConfig());
    }

    /**
     * 无参构造函数
     */
    public FormTableColumnDTO(){
        this.columnInfoDTOList = new ArrayList<>();
    }
    

    /**
     * 解析表单模板内容，提取字段信息
     * @param content 表单模板的JSON内容
     * @return 字段信息列表
     */
    private List<FormColumnInfoDTO> parseContentToColumnInfoList(String content) {
        if (content == null || content.trim().isEmpty()) {
            return new ArrayList<>();
        }
        try {
            /// 将设计内容转换成json
            Map<String, Object> contentMap = new ObjectMapper().readValue(content, new TypeReference<Map<String, Object>>(){});
            @SuppressWarnings("unchecked")/// 去除代码告警
            List<Map<String, Object>> fields = (List<Map<String, Object>>) contentMap.get("components");
            if (fields == null) {
                return new ArrayList<>();
            }
            
            return fields.stream()
                    .filter(field -> field.get("name") != null && field.get("label") != null)
                    .map(this::buildColumnInfo)
                    .collect(java.util.stream.Collectors.toList());
                    
        } catch (Exception e) {
            // 解析失败时返回空列表，避免影响其他功能
            return new ArrayList<>();
        }
    }
    
    private FormColumnInfoDTO buildColumnInfo(Map<String, Object> field) {
        String fieldType = (String) field.get("type");
        Boolean required = (Boolean) field.get("required");
        Object defaultValueObj = field.get("defaultValue");
        String defaultValue = null;
        
        // 处理defaultValue的类型转换
        if (defaultValueObj != null) {
            defaultValue = String.valueOf(defaultValueObj);
        }
        
        return FormColumnInfoDTO.builder()
                .columnName((String) field.get("name"))
                .columnComment((String) field.get("label"))
                .columnType(FormFieldTypeEnum.getDbType(fieldType))
                .formType(fieldType)
                .columnLength(FormFieldTypeEnum.getColumnLength(fieldType))
                .columnScale(FormFieldTypeEnum.getColumnScale(fieldType))
                .isNullable(required == null || !required)
                .defaultValue(defaultValue)
                .isPrimaryKey(false)
                .isAutoIncrement(false)
                .build();
    }







}
